package com.feng.controller;

import com.feng.pojo.Book;
import com.feng.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier(value = "BookServiceImpl")
    private BookService bookService;

    /**
     * 查询所有书籍
     * @param model
     * @return
     */
    @RequestMapping("/allBook")
    public String allBook(Model model){

        List<Book> list = bookService.queryBooks();
        model.addAttribute("list",list);
        return  "bookWeb";
    }

    /**
     * 书名查找
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/quaryByName")
    public String bookByName( String name,Model model){
        System.out.println(name);
        List<Book> list = bookService.queryBooksByName(name);
        model.addAttribute("list",list);
        return  "bookWeb";
    }

    /**
     * 删除图书信息
     * @param id
     * @return
     */
    @RequestMapping("/del/{id}")
    public String deleteBook(@PathVariable("id") int id){
        bookService.deleteBook(id);
        return "forward:/book/allBook";
    }

    /**
     * 转向修改页面
     * @param id
     * @return
     */
    @RequestMapping("/toAlter/{id}")
    public String alter(@PathVariable("id") int id,Model model){
        Book book = bookService.quaryBookById(id);
        model.addAttribute("Book",book);
        return  "alter";
    }

    /**
     * 修改书籍信息
     * @param book
     * @return
     */
    @RequestMapping("/update")
    public String change(Book book){
        System.out.println("Book======"+book);
        int i = bookService.updateBook(book);
        return  "forward:/book/allBook";
    }

    /**
     * 转向添加书籍页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "addBooks";
    }

    /**
     * 增加书籍
     * @param book
     * @return
     */
    @RequestMapping("/addBook")
    public  String Add(Book book){
        System.out.println("添加图书");
        bookService.addBook(book);
        return "forward:/book/allBook";
    }


}
